<?php

namespace App\Http\Controllers;

use App\Department;
use App\Http\Requests;
use App\Order;
use App\Product;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     * @author 邱全武
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     * @author 邱全武
     * @return \Illuminate\Http\Response 登录后主界面
     */
    public function index()
    {
        return view('home');
    }

    /*
     * @author 邱全武
     * @return 重置密码页面
     */
    public function reset()
    {
        return view('user.reset');
    }

    public function departmentList()
    {
        if (Auth::user()->department_type == '1') {
            return '没有权限';
        } else {
            $department = Department::get();
            return view('department.list', [
                'department' => $department
            ]);
        }
    }

    //新增部门操作
    public function departmentAdd(Request $request)
    {
        if (Auth::user()->department_type == '1') {
            return '没有权限';
        } else {


            if ($request->isMethod('GET')) {
                //判断请求类型,当为get请求时,返回增加部门页面
                return view('department.add');
            } elseif ($request->isMethod('POST')) {
                //当为post请求时,执行新增部门操作
                $department_type = $request->input('departmentType');
                $department_name = $request->input('departmentName');
                if ($department_name == "" or $department_type == "") {
                    return back()->withErrors('部门名称不能为空!');
                } else {
                    $department = new Department();
                    $department->department_name = $department_name;
                    $department->department_type = $department_type;
                    if ($department->save()) {
                        return redirect('department_list')->withErrors('新增部门成功!');
                    } else {
                        return back()->withErrors('出现异常错误!这个错误直接导致了新增部门失败,有可能是本人没有考虑的bug,也有可能是数据库出现了错误!');
                    }
                }
            }

        }
    }

    //执行删除部门操作
    public function departmentDelete($id)
    {
        if (Auth::user()->department_type == '1') {
            return '没有权限';
        } else {
            $department = Department::find($id);
            if ($department->delete()) {
                return redirect('department_list')->withErrors('删除部门成功!');
            } else {
                return redirect('department_list')->withErrors('删除部门失败!');
            }
        }
    }

    //执行部门更新操作
    public function departmentUpdate(Request $request, $id)
    {
        //判断请求类型
        if (Auth::user()->department_type == '1') {
            return '没有权限';
        } else {
            if ($request->isMethod('GET')) {
                $department = Department::find($id);
                return view('department.update', [
                    'department' => $department
                ]);
            } elseif ($request->isMethod('POST')) {
                //当为post请求时,执行更新部门操作
                $department_type = $request->input('departmentType');
                $department_name = $request->input('departmentName');
                if ($department_name == "" or $department_type == "") {
                    return back()->withErrors('部门名称不能为空!');
                } else {
                    $department = Department::find($id);
                    $department->department_name = $department_name;
                    $department->department_type = $department_type;
                    if ($department->save()) {
                        return redirect('department_list')->withErrors('更新部门成功!');
                    } else {
                        return back()->withErrors('出现异常错误!这个错误直接导致了更新部门失败,有可能是本人没有考虑的bug,也有可能是数据库出现了错误!');
                    }
                }
            }
        }
    }

    public function userList()
    {
        if (Auth::user()->department_type == '1') {
            return '没有权限';
        } else {
            $user = User::get();
            return view('user.list', [
                'user' => $user
            ]);
        }
    }

    public function userResetPassword($id)
    {
        if (Auth::user()->department_type == '1') {
            return '没有权限';
        } else {
            $user = User::find($id);
            $user->password = bcrypt('123456');
            $message = "成功重置" . $user->email . "的密码为123456";
            if ($user->save()) {
                return back()->withErrors($message);
            } else {
                return back()->withErrors('出现异常错误!这个错误直接导致了重置密码失败,有可能是本人没有考虑的bug,也有可能是数据库出现了错误!');
            }

        }
    }

    public function userDelete($id)
    {
        if (Auth::user()->department_type == '1') {
            return '没有权限';
        } else {
            $user = User::find($id);
            if ($user->delete()) {
                return redirect('user_list')->withErrors('删除用户成功!');
            } else {
                return redirect('user_list')->withErrors('删除用户失败!');
            }
        }
    }

    /**
     * @param Request $request
     * @return $this|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function userAdd(Request $request)
    {
        if (Auth::user()->department_type == '1') {
            return '没有权限';
        } else {
            if ($request->isMethod('GET')) {
                $department = Department::get();
                return view('user.add', [
                    'department' => $department
                ]);
            } elseif ($request->isMethod('POST')) {
                $user_name = $request->input('userName');
                $department_name = $request->input('departmentName');
                if ($department_name == "" or $user_name == "") {
                    return back()->withErrors('用户姓名不能为空!');
                } else {
                    $department_type = Department::find($department_name)->department_type;
                    User::create([

                        'email' => $user_name,
                        'password' => bcrypt('123456'),
                        'department' => $department_name,
                        'department_type' => $department_type
                    ]);
                    return back()->withErrors('新增用户成功,初始密码为123456');
                }

            } else {
                return back()->withErrors('新增用户失败');
            }
        }
    }

    public function productList()
    {
        if (Auth::user()->department_type == '1') {
            return '没有权限';
        } else {
            $product = Product::get();
            return view('product.list', [
                'product' => $product
            ]);
        }
    }

    public function productDelete($id)
    {
        if (Auth::user()->department_type == '1') {
            return '没有权限';
        } else {
            $product = Product::find($id);
            if ($product->delete()) {
                return redirect('product_list')->withErrors('删除记录成功!');
            } else {
                return redirect('product_list')->withErrors('删除记录失败!');
            }
        }
    }

    public function productAdd(Request $request)
    {
        if (Auth::user()->department_type == '1') {
            return '没有权限';
        } else {
            if ($request->isMethod('GET')) {
                //判断请求类型,当为get请求时,返回增加部门页面
                return view('product.add');
            } elseif ($request->isMethod('POST')) {
                $name = $request->input('productName');
                $type = $request->input('productType');
                $price = $request->input('productPrice');
                $number = $request->input('productNumber');
                $data = $request->all();
                //验证规则
                $rules = [
                    'productName' => 'required|between:0,20',
                    'productType' => 'required|between:0,20',
                    'productPrice' => 'required|integer',
                    'productNumber' => 'required|integer'
                ];
                //错误提示
                $messages = [
                    'required' => '选项不能为空',
                    'between' => '名称和类型必须是0~20位之间',
                    'integer' => '价格和数量必须为整数'
                ];
                $validator = Validator::make($data, $rules, $messages);
                if ($validator->fails()) {
                    return back()->withErrors($validator);  //返回一次性错误
                };
                Product::create([
                    'name' => $name,
                    'type' => $type,
                    'price' => $price,
                    'number' => $number
                ]);
                return back()->withErrors('添加数据成功');  //返回一次性错误
            }

        }
    }

    public function add(Request $request)
    {
        if (Auth::user()->department_type == '1') {
            return '没有权限';
        } else {
            if ($request->isMethod('POST')) {
                $id = $request->input('id');
                $num = $request->input('productNumber');
                $product = Product::find($id);
                $product->number = $num;
                if (preg_match("/^\d*$/", $num) and $num!=="") {
                    if ($product->save()) {
                        return back()->withErrors('修改数量成功');
                    } else {
                        return back()->withErrors('修改数量失败,请检查数据是否能正常访问?');
                    }
                } else {
                    return back()->withErrors('请检查输入数量的是否为数字.');
                }

            }
        }

    }
    //返回待审核的列表
    public function orderList0(){
        if (Auth::user()->department_type == '1') {
            return '没有权限';
        } else {
            $order=Order::where('state',0)->get();
            return view('order.list',[
                'order'=>$order
            ]);
        }
    }
    public function orderList1(){
        if (Auth::user()->department_type == '1') {
            return '没有权限';
        } else {
            $order=Order::whereBetween('state',[1,2])->get();
            return view('order.list1',[
                'order'=>$order
            ]);
        }
    }
    //审核操作
    public function approvalOrder($id){
        if (Auth::user()->department_type == '1') {
            return '没有权限';
        } else {
            $order=Order::find($id);
            if($order->state!=1){
                $order->state=1;
                $order->save();
                return back()->withErrors('审核成功');
            }else{
                return '操作失败,该数据已经被审核!';
            }
        }
    }
    //拒绝操作
    public function orderReject(Request $request){
        if (Auth::user()->department_type == '1') {
            return '没有权限';
        } else {
            $id = $request->input('order_id');
            $rejected=$request->input('rejected');
            $order=Order::find($id);
            if($order->state!=3){
                $order->state=3;
                $order->rejected=$rejected;
                $order->save();
                return back()->withErrors('拒绝成功');
            }else{
                return '操作失败,该数据已经被拒绝';
            }
        }
    }
    //发放操作
    public function orderGrant($id){
        if (Auth::user()->department_type == '1') {
            return '没有权限';
        } else {

            $order=Order::find($id);
            if($order->state!=2){
                $order->state=2;
                $order->save();
                return back()->withErrors('操作成功');
            }else{
                return '操作失败,该数据已经被处理';
            }
        }
    }
    //录入列表
    public function inputList()
    {
        if (Auth::user()->department_type == '0') {
            return '没有权限';
        } else {
            $product = Product::get();
            return view('input.list', [
                'product' => $product
            ]);
        }
    }
    //录入操作
    public function inputAction(Request $request){
        if (Auth::user()->department_type == '0') {
            return '没有权限';
        } else {
           $product_id=$request->input('id');
           $number=$request->input('productNumber');
           $department=Auth::user()->department;
           $user_id=Auth::user()->id;
           $state=0;
           $explain=$request->input('explain');
           if($number==""){
               return back()->withErrors('请输入录入数量,且不能超过库存');
           }else{
               $order=new Order();
               $order->product_id=$product_id;
               $order->department=$department;
               $order->number=$number;
               $order->user_id=$user_id;
               $order->state=$state;
               $order->explain=$explain;
               $product=Product::find($product_id);
               $product->number=$product->number-$number;
               if($product->number>0){
                   $product->save();
                  $order->save();
                   return back()->withErrors('录入成功');
               }else{
                   return back()->withErrors('录入失败,录入数量不能超过库存');
               }

           }

        }
    }
    //已录入列表
    public function inputList1()
    {
        if (Auth::user()->department_type == '0') {
            return '没有权限';
        } else {
            $department=Auth::user()->department;
            $order = Order::where('department',$department)->get();
            return view('input.list1', [
                'order' => $order
            ]);
        }
    }
}
